package org.szgb.console.sys.line.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.sys.system.entity.SysSystem;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysLineLocationSystem extends BeanEntity<SysLineLocationSystem> {
    @Serial
    private static final long serialVersionUID = 1L;
    private SysLineLocation sysLineLocation;
    private SysSystem sysSystem;

    public SysLineLocationSystem() {
    }

    public SysLineLocationSystem(String id) {
        this.setId(id);
    }

    public SysLineLocationSystem(SysLineLocationSystem.SysLineLocationSystemFindDto sysLineLocationSystemFindDto) {
        this.setId(sysLineLocationSystemFindDto.getId());
        this.setSysSystem(new SysSystem(sysLineLocationSystemFindDto.getSysSystemId()));
        this.setSysLineLocation(new SysLineLocation(sysLineLocationSystemFindDto.getSysLineLocationId()));
        this.setPageDto(sysLineLocationSystemFindDto.getPageDto());
    }

    public SysLineLocationSystem(SysLineLocationSystem.SysLineLocationSystemSaveDto sysLineLocationSystemSaveDto) {
        this.setId(sysLineLocationSystemSaveDto.id);
        this.setSysSystem(new SysSystem(sysLineLocationSystemSaveDto.getSysSystemId()));
        this.setSysLineLocation(new SysLineLocation(sysLineLocationSystemSaveDto.getSysLineLocationId()));
    }

    public SysLineLocationSystem.SysLineLocationSystemView toSysLineLocationSystemView() {
        return new SysLineLocationSystem.SysLineLocationSystemView(this);
    }

    @Data
    @Schema(title = "线路站点系统信息", description = "线路站点系统信息")
    public static class SysLineLocationSystemView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "站点id")
        private String sysLineLocationId;
        @Schema(description = "站点名称")
        private String sysLineLocationName;
        @Schema(description = "系统id")
        private String sysSystemId;
        @Schema(description = "系统名称")
        private String sysSystemName;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysLineLocationSystemView(SysLineLocationSystem sysLineLocationSystem) {
            this.id = sysLineLocationSystem.getId();
            this.sysLineLocationId = sysLineLocationSystem.getSysLineLocation().getId();
            this.sysLineLocationName = sysLineLocationSystem.getSysLineLocation().getName();
            this.sysSystemId = sysLineLocationSystem.getSysSystem().getId();
            this.sysSystemName = sysLineLocationSystem.getSysSystem().getName();
            this.createdAt = sysLineLocationSystem.getCreatedAt();
            this.updatedAt = sysLineLocationSystem.getUpdatedAt();
            if (sysLineLocationSystem.getCreatedBy() != null) {
                this.createdById = sysLineLocationSystem.getCreatedBy().getId();
                this.createdByName = sysLineLocationSystem.getCreatedBy().getName();
            }
            if (sysLineLocationSystem.getUpdatedBy() != null) {
                this.updatedById = sysLineLocationSystem.getUpdatedBy().getId();
                this.updatedByName = sysLineLocationSystem.getUpdatedBy().getName();
            }
        }

        public static List<SysLineLocationSystem.SysLineLocationSystemView> getViewListFromList(List<SysLineLocationSystem> sysLineLocationSystemList) {
            return sysLineLocationSystemList.stream().map(SysLineLocationSystem.SysLineLocationSystemView::new).toList();
        }
    }

    @Data
    @Schema(title = "线路站点系统树表子项信息", description = "线路站点系统树表子项信息")
    public static class SysLineLocationSystemTreeView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "系统名称")
        private String sysSystemName;
        @Schema(description = "设备树")
        private List<SysLineLocationSystemEquipment.SysLineLocationSystemEquipmentTreeView> equipmentTree;

        public SysLineLocationSystemTreeView(SysLineLocationSystem sysLineLocationSystem, List<SysLineLocationSystemEquipment.SysLineLocationSystemEquipmentTreeView> equipmentTree) {
            this.id = sysLineLocationSystem.getId();
            this.sysSystemName = sysLineLocationSystem.getSysSystem().getName();
            this.equipmentTree = equipmentTree;
        }
    }

    @Data
    @Schema(title = "线路站点系统查询表单", description = "线路站点系统查询参数")
    public static class SysLineLocationSystemFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "站点id")
        private String sysLineLocationId;
        @Schema(description = "系统id")
        private String sysSystemId;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysLineLocationSystem toSysLineLocationSystem() {
            return new SysLineLocationSystem(this);
        }
    }

    @Data
    @Schema(title = "线路站点系统保存表单", description = "线路站点系统保存参数")
    public static class SysLineLocationSystemSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "站点id")
        private String sysLineLocationId;
        @Schema(description = "系统id")
        private String sysSystemId;

        public SysLineLocationSystem toSysLineLocationSystem() {
            return new SysLineLocationSystem(this);
        }
    }
}
